package com.ruoyi.system.rabbitMqListener;


import com.rabbitmq.client.Channel;
import com.ruoyi.system.domain.TbFaultLog;
import com.ruoyi.system.service.ITbFaultLogService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.UUID;

@Component
public class FaultListener {

    @Autowired
    private ITbFaultLogService tbFaultLogService;

    static Integer num=0;

    @RabbitHandler
    @RabbitListener(queues = "2010a")
    public void saveFaultLog(Object data, Channel channel ) throws IOException {

        String text= (String) data;
        String flag=text.substring(0,text.indexOf(","));
        String vin=text.substring(text.indexOf(",")+1);

        int i = RandomUtils.nextInt();
        Integer a=i;
        long l = a.longValue();

        TbFaultLog tbFaultLog=new TbFaultLog();
        tbFaultLog.setFaultLogId(l);
        tbFaultLog.setFaultCode(flag);
        tbFaultLog.setCarCode(vin);
        tbFaultLogService.insertTbFaultLog(tbFaultLog);

        channel.basicAck(++num,false);
    }



}
